package org.brisling.medical.queryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.WhereUtil;
import org.brisling.medical.domain.MedicalPathwayBaseinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/pathwayBaseinfo/query")
public class QueryMedicalPathwayBaseinfo extends AbstractBaseQueryService<MedicalPathwayBaseinfo>{
	
	
	
	public QueryMedicalPathwayBaseinfo(){
		this.moduleName = ConstSysParam.MODULE_PATHWAY_BASEINFO;
		this.subSystem = ConstSysParam.SUBSYSTEM_MEDICAL;
		this.tableName = this.moduleName;
		this.clazz = MedicalPathwayBaseinfo.class;
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("apply_deptname","");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<MedicalPathwayBaseinfo> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap, true);
		
	}
	
	@RequestMapping("/getList.action")
	public List<MedicalPathwayBaseinfo> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	public List<MedicalPathwayBaseinfo> getListAll(Map<String, String> map){	
		
		String sql = "";
		String whereCorse = null;					
			
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		System.out.println("whereCorse: " +whereCorse);		
		
		if(whereCorse!=null){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
			return super.getAllList(sql);
		}
		return new ArrayList<MedicalPathwayBaseinfo>();		
		
	}
	/**
	 * 根据指标id查找指标信息
	 * @param id	指标id
	 * @return 指标信息记录 
	 */
	@RequestMapping("/getOne.action")
	public MedicalPathwayBaseinfo getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据指标唯一编码查找指标信息
	 * @param identify	指标唯一编码
	 * @return 指标信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public MedicalPathwayBaseinfo getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<MedicalPathwayBaseinfo> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentify(field,fieldVal);
	}
}
